package com.dushibao.dao;

import com.dushibao.common.Constants;
import com.dushibao.entity.WebSite;
import com.dushibao.utils.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Types;

public class WebSiteDao {

    public WebSite getOne() {
        try{
            StringBuilder sql = new StringBuilder();
            sql.append(" select ");
            sql.append(" w.id as wid,");
            sql.append(" w.addTime as waddTime,");
            sql.append(" w.website_name as websiteName,");
            sql.append(" w.poweredby as poweredby,");
            sql.append(" w.company_name as companyName,");
            sql.append(" w.website_title as websiteTitle,");
            sql.append(" w.website_logo as websiteLogo,");
            sql.append(" r.path as path");
            sql.append(" from dushibao_website w left join dushibao_resource r on w.website_logo = r.id ");

            Connection conn = DBUtils.getConnection();
            PreparedStatement stmt = conn.prepareStatement(sql.toString());

            ResultSet rs = stmt.executeQuery();
            WebSite webSite = new WebSite();

            if(rs.next()){
                webSite.setId(rs.getLong("wid"));
                webSite.setAddTime(rs.getTimestamp("waddTime").toLocalDateTime());
                webSite.setWebsiteName(rs.getString("websiteName"));
                webSite.setPoweredBy(rs.getString("poweredby"));
                webSite.setCompanyName(rs.getString("companyName"));
                webSite.setWebsiteTitle(rs.getString("websiteTitle"));
                webSite.setWebsiteLogo(rs.getLong("websiteLogo"));
                webSite.setLogo(rs.getString("path"));
            }

            return webSite;
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    public Integer save(WebSite webSite) {

        try{
            String sql = "insert into dushibao_website(website_name,poweredby,company_name,website_title,website_logo) values(?,?,?,?,?)";

            Connection conn = DBUtils.getConnection();
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setString(1,webSite.getWebsiteName());
            stmt.setString(2,webSite.getPoweredBy());
            stmt.setString(3,webSite.getCompanyName());
            stmt.setString(4,webSite.getWebsiteTitle());
            stmt.setLong(5,webSite.getWebsiteLogo());
            stmt.executeUpdate();
            return Constants.SUCCESS;
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    public Integer update(WebSite webSite) {
        try{
            String sql = "update dushibao_website set website_name=?,poweredby=?,company_name=?,website_title=?,website_logo=? where id=?";
            Connection conn = DBUtils.getConnection();
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setString(1,webSite.getWebsiteName());
            stmt.setString(2,webSite.getPoweredBy());
            stmt.setString(3,webSite.getCompanyName());
            stmt.setString(4,webSite.getWebsiteTitle());
            if(webSite.getWebsiteLogo()==null){
                stmt.setNull(5,Types.BIGINT);
            }else{
                stmt.setLong(5,webSite.getWebsiteLogo());
            }
            stmt.setLong(6,webSite.getId());

            stmt.executeUpdate();
            return Constants.SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}
